# -*- coding: utf-8 -*-
import requests
from sina_数据.欧美.欧美1H数据分析 import showChart
import time


# 爬取外汇数据
def get_data_from_sina(Symbol='usdcad'):
    """此方法是转为爬取来自新浪财经的数据而设计的"""

    url_harf = "https://vip.stock.finance.sina.com.cn/forex/api/jsonp.php/var%20_fx_s" + Symbol + "_1_1580278725141=/NewForexService.getMinKline?symbol=fx_s" + Symbol + "&scale=1&datalen=1440"
    # url = "https://vip.stock.finance.sina.com.cn/forex/api/jsonp.php/var%20_fx_seurusd_1_1580278725141=/NewForexService.getMinKline?symbol=fx_seurusd&scale=1&datalen=1440"
    url = url_harf
    headers = {
        "User-Agent": "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/75.0.3770.100 Safari/537.36"
    }

    response = requests.get(url=url, headers=headers)

    for i, j in zip(response.text.replace('"',""), range(90000)):
        if i == "(":
            text = response.text[j + 1:-1].replace("d", '"d"').replace("o", '"o"').replace("l", '"l"').replace("h",
                                                                                                               '"h"').replace(
                "c", '"c"').replace("p", '"p"')
            break
    text = eval(text)

    # 把数据反过来
    text_ = []
    for i in range(len(text)):
        text_.insert(0, text[len(text) - i - 1])
    text = text_
    return text




if __name__ == '__main__':
    # 开盘    最低    最高    收盘
    a = get_data_from_sina()
    print(a)


